import java.util.Vector;

class FuzzyRuleSA{
    protected String id;
    protected Vector<FuzzyVariableSA> v_variables;

    public FuzzyRuleSA(){
	this.v_variables = new Vector();
    }//end constructor
    
    public FuzzyRuleSA(String id){
	this.id = id;
	this.v_variables = new Vector();
    }//end constructor
    
    public String getId(){return this.id;}

    public String toString(){
	String out =""; 
	for(int i=0; i < this.v_variables.size();i++){
	    FuzzyVariableSA fv = v_variables.elementAt(i);
	    if(fv.getNumberFuzzySets() == 0)
		out += "{_};";
	    else{
		for(int j=0;j<fv.getNumberFuzzySets();j++){
		    FuzzySetSA fs = fv.getFuzzySetAt(j);
		    out += "("+fs.getA()+","+fs.getB()+","+fs.getC()+","+fs.getD()+")";
		}//end for - while there are fuzzy sets
		out+=";";
	    }
	}//end for, while there are variables 
	return out;	
    }
    public void addVariable(FuzzyVariableSA fv){
	this.v_variables.add(fv);
    }

    public FuzzyVariableSA getFuzzyVariableAt(int pos){return this.v_variables.elementAt(pos);}

    public float membershipValue(Vector v_values){
	float result = 1;
	//--------- VARIABLES OF THE CURRENT RULE -----------
	for(int i=0;i<v_variables.size();i++){
	    FuzzyVariableSA fv = this.v_variables.elementAt(i);
	    float value = (Float) v_values.elementAt(i);
	    boolean belongs = false;
	 

	    if(fv.getNumberFuzzySets()>0){			
		//--------- FUZZY SETS OF THE CURRENT VARIABLE -------
		for(int j=0;j<fv.getNumberFuzzySets();j++){
		    FuzzySetSA fs = fv.getFuzzySetAt(j);
		    float tmp = fs.membershipValue(value);
		    if(tmp>0){
			belongs = true;
			if(tmp < result) result = tmp;
			break;
		    }			    
		}//end for
		//--------END FUZZY SETS -----------------------------
		if(!belongs){
		   return 0;
		}
	    }//end if
	}//end for variables		
	//------------END ANALYSIS OF VARIABLES FOR A RULE -----------
	return result;
    }//end membershipValue method
}//end class
